/**
 * 添加或者修改页面
 */
var LxDdWzhInfoDlg = {
    data: {
        ddDdbh: "",
        ddTxm: "",
        ddTime: "",
        ddSl: "",
        ddZj: "",
        ddUser: "test"
    }
};


layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;


    var selectedData = JSON.parse(sessionStorage.getItem("selectedData"));
    if (selectedData) {
        console.log(selectedData);
        // TODO: 根据需要对数据进行处理

        const currentDate = new Date();
        const year = String(currentDate.getFullYear()).padStart(4, '0');
        const month = String(currentDate.getMonth() + 1).padStart(2, '0');
        const day = String(currentDate.getDate()).padStart(2, '0');
        const hours = String(currentDate.getHours()).padStart(2, '0');
        const minutes = String(currentDate.getMinutes()).padStart(2, '0');
        const seconds = String(currentDate.getSeconds()).padStart(2, '0');
        const dateTimeString = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;



        var min = 10;
        var max = 20;
        var randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
        var filteredData = {
            ddDdbh: (selectedData[0].spTxm) + 'x' + randomInt,
            ddTxm: selectedData[0].spTxm,
            ddTime: dateTimeString,
            ddZj: selectedData[0].spLings,
            spChuo: selectedData[0].spChuo
            // 可根据需求添加其他属性
        };

        // 给表单元素赋值
        $('#ddDdbh').val(filteredData.ddDdbh);
        $('#ddTxm').val(filteredData.ddTxm);
        $('#ddSl').val();
        $('#ddTime').val(filteredData.ddTime);
        $('#ddZj').val(filteredData.ddZj);
        $('#ddUser').val(LxDdWzhInfoDlg.data.ddUser);


        sessionStorage.removeItem("selectedData");
    }

    //让当前iframe弹层高度适应
    admin.iframeAuto();
        //表单提交事件
        form.on('submit(btnSubmit)', function (data) {
            var inputValue = $("#ddSl").val();
            var num = parseFloat(inputValue);
            if (inputValue >= 0  && filteredData.spChuo - inputValue >= 0 && Number.isInteger(num) ) {
                    var ajax = new $ax(Feng.ctxPath + "/lxDdWzh/addItem", function (data) {
                        Feng.success("添加成功！");
                        window.location.href = Feng.ctxPath + '/lxSpWzh'
                    }, function (data) {
                        Feng.error("添加失败！" + data.responseJSON.message + "请核对输入信息")
                    });
                    ajax.set(data.field);
                    ajax.start();

                    return false;
            }else {
                Feng.error("添加失败！数量错误请返回购买页面")
                window.location.href = Feng.ctxPath + '/lxSpWzh'
                return false;
            }
        });

        //返回按钮
        $("#backupPage").click(function () {
            window.location.href = Feng.ctxPath + '/lxSpWzh'
        });
    });

